package com.zzf.networktest

import kotlinx.coroutines.*
import kotlin.coroutines.suspendCoroutine

class CoroutinesTest {

}


fun main(args:Array<String>){

    testAsyncAwait2()
    testMax()
}

fun textRunblocking(){
    println("主线程开始运行")

    runBlocking {
        launch {
            println("代码1")
            delay(1000)
            println("代码1完成")
        }

        launch {
            println("代码2")
            delay(10)
            println("代码2完成")
        }
    }

    println("主线程结束运行")
}

fun testAsyncAwait(){
    runBlocking {
        val start = System.currentTimeMillis()
        val result1 = async {
            delay(1000)
            5+5
        }.await()

        val result2 = async {
            delay(1000)
            4+6
        }.await()

        println("结果是:${result1 + result2}")
        val end = System.currentTimeMillis()
        println("耗时:${end - start}ms")
    }
}

fun testAsyncAwait2(){
    runBlocking {
        val start = System.currentTimeMillis()
        val result1 = async {
            delay(1000)
            5+5
        }

        val result2 = async {
            delay(1000)
            4+6
        }

        println("结果是:${result1.await() + result2.await()}")
        val end = System.currentTimeMillis()
        println("耗时:${end - start}ms")
    }
}

fun testMax(){
    val largest = max(1,2,3,4,5,6)
    print("最大值是:$largest")

    val largest2 = max(1.5,2.9,3.8,4.7,5.7,6.6)
    print("最大值2是:$largest2")


}